﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class ChiTietGioHoa : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            DataTable temp = (DataTable)Session["hoa"];
            GridView1.DataSource =temp;
            GridView1.DataBind();
            if (temp == null)
                MultiView1.SetActiveView(View2);
            else
            {
                MultiView1.ActiveViewIndex = 0;
                
                lblthanhtien.Text = temp.Compute("sum(thanhtien)", "").ToString();
                Session["tien"] = temp.Compute("sum(thanhtien)", "").ToString();
            }
        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        DataTable temp = (DataTable)Session["hoa"];
        if (temp == null)
            MultiView1.SetActiveView(View2);
        else
        {
            MultiView1.ActiveViewIndex = 0;
            temp.Rows[e.RowIndex].Delete();
            GridView1.DataSource = temp;
            GridView1.DataBind();
            lblthanhtien.Text = temp.Compute("sum(thanhtien)", "").ToString();
            Response.Redirect(Request.Path);
        }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        DataTable temp = (DataTable)Session["hoa"];
        
        GridView1.DataSource = temp;
        GridView1.DataBind();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        DataTable temp = (DataTable)Session["hoa"];
        GridView1.DataSource = temp;
        GridView1.DataBind();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        DataTable temp = (DataTable)Session["hoa"];
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            try
            {
                TextBox sl = (TextBox)GridView1.Rows[i].Cells[2].Controls[0];
                temp.Rows[i]["soluong"] = sl.Text;
            }
            catch (Exception)
            {
            }
        }

        GridView1.EditIndex = -1; 
        GridView1.DataSource = temp;
        GridView1.DataBind();
        lblthanhtien.Text = temp.Compute("sum(thanhtien)", "").ToString();
        
       
    }
    giohoa gh = new giohoa();
    protected void lbtnthanhtoan_Click(object sender, EventArgs e)
    {
        DataTable temp = (DataTable)Session["hoa"];
        if (Session["kh"] != null)
        {
            string ten = Session["kh"].ToString();
            DateTime ngayxuat = DateTime.Now.Date;
            string mahdx = gh.laysohdx(ten, ngayxuat);

            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                string hoa = GridView1.DataKeys[i].Values[0].ToString();
                temp.Rows[i]["tenhoa"] = hoa;
                string soluong = GridView1.DataKeys[i].Values[1].ToString();
                temp.Rows[i]["soluong"] = soluong;
                string gia = GridView1.DataKeys[i].Values[2].ToString();
                temp.Rows[i]["giahoa"] = gia;


                gh.Insert_CTHDX(mahdx, hoa, soluong, gia);
            }
            Response.Redirect("~/ThanhToanGioHoa.aspx");
        }
        else
            Response.Redirect("~/DangNhap.aspx");
    }
    protected void GridView1_Load(object sender, EventArgs e)
    {
       lblthanhtien.Text="test thui";
    }

}
